package com.dingreading.cloud.admin.controller;

import com.dingreading.cloud.admin.dto.BalanceLogDto;
import com.dingreading.cloud.admin.entity.BalanceLog;
import com.dingreading.cloud.admin.entity.OrderForm;
import com.dingreading.cloud.admin.entity.Staff;
import com.dingreading.cloud.admin.service.BalanceLogService;
import com.dingreading.cloud.admin.service.OrderFormService;
import com.dingreading.cloud.admin.util.UidUtil;
import com.dingreading.cloud.common.base.R;
import com.dingreading.cloud.common.util.PageBeanDto;
import com.dingreading.cloud.common.util.PageUtil;
import com.mybatisflex.core.paginate.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

@RestController
@RequestMapping("/system/balanceLog")
@Api(tags = "总部-收支流水记录")
public class BalanceLogController {

    @Resource
    private BalanceLogService balanceLogService;
    @Resource
    private OrderFormService orderFormService;


    @ApiOperation(value = "分页列表")
    @PostMapping(value = "/pageList", produces = "application/json; charset=UTF-8")
    public R<Object> pageList(
            @ApiParam(value = "总部员工uid") @RequestParam(value = "headStaffUid") String headStaffUid,
            @ApiParam(value = "总部员工名") @RequestParam(value = "headStaffName") String headStaffName,
            @ApiParam(value = "分页公共类") PageUtil pageUtil,
            @ApiParam(value = "流水号/关联订单号/手机号") @RequestParam(value = "keyword", required = false) String keyword,
            @ApiParam(value = "项目类型") @RequestParam(value = "type", required = false) Integer type,
            @ApiParam(value = "收支类型 1.收入 2.支出") @RequestParam(value = "income", required = false) Integer income,
            @ApiParam(value = "支付类型 1微信,2支付宝") @RequestParam(value = "payType", required = false) Integer payType,
            @ApiParam(value = "状态 -1.已退费 0.取消 1.正常") @RequestParam(value = "status", required = false) Integer status,
            @ApiParam(value = "支付开始时间 yyyy-MM-dd") @RequestParam(value = "beginTime", required = false) String beginTime,
            @ApiParam(value = "支付结束时间 yyyy-MM-dd") @RequestParam(value = "endTime", required = false) String endTime
    ) {
        Page<BalanceLog> page = this.balanceLogService.pageList(pageUtil, type, income, keyword, payType,
                status, beginTime, endTime);

        PageBeanDto data = new PageBeanDto();
        data.setTotal(page.getTotalRow());
        data.setList(page.getRecords());
        return R.ok(data);
    }


    @ApiOperation(value = "获取收支记录详情")
    @PostMapping(value = "/get", produces = "application/json; charset=UTF-8")
    public R<Object> get(
            @ApiParam(value = "总部员工uid") @RequestParam(value = "headStaffUid") String headStaffUid,
            @ApiParam(value = "总部员工名") @RequestParam(value = "headStaffName") String headStaffName,
            @ApiParam(value = "员工uid") @RequestParam(value = "staffUid") String staffUid,
            @ApiParam(value = "流水号") @RequestParam(value = "code") String code
    ) {
        Object staffObj = UidUtil.getStaff(staffUid);
        if (staffObj instanceof String) return R.fail(staffObj.toString());
        Staff staff = (Staff) staffObj;

        BalanceLog log = balanceLogService.getByCode(code);
        if (log == null) return R.fail("收支记录获取失败");

        OrderForm form = orderFormService.getByCode(staff.getUid(), log.getOrderCode());

        BalanceLogDto dto = new BalanceLogDto();
        dto.setLog(log);
        dto.setForm(form);

        return R.ok(dto);
    }

}
